package collections2;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;
import java.util.ResourceBundle;

 
/**
 * 获取*.properties配置文件中的内容 ，常见的两种方法：
 * 
 * @author 冰雨凌風
 * 
 */
public class ReadProperties {
    // 方法一

    /**public void One() {
        // 获得资源包
        ResourceBundle bundle = ResourceBundle.getBundle("test");
        // 通过资源包拿到所有的名称
        Enumeration<String> allName = bundle.getKeys();
        // 遍历
        while (allName.hasMoreElements()) {
            // 获取每一个名称
            String name = (String) allName.nextElement();
            // 利用已得到的名称通过资源包获得相应的值
            String value = bundle.getString(name);
            System.out.println(name + "=" + value);
        }
    }
 
    // 方法二
   
    public void Two() throws Exception {
        // 获得类加载器，然后把文件作为一个流获取
        InputStream in = ReadProperties.class.getClassLoader().getResourceAsStream("test.properties");
        // 创建Properties实例
        Properties prop = new Properties();
        // 将Properties和流关联
        prop.load(in);
        // 获取所有的名称
        Enumeration<?> allName = prop.propertyNames();
        // 遍历
        while (allName.hasMoreElements()) {
            // 获得每一个名称
            String name = (String) allName.nextElement();
            // 利用已得到的名称通过Properties对象获得相应的值
            String value = (String) prop.get(name);
            System.out.println(name + "=" + value);
        }
        // 关闭资源
        in.close();
    }
}*/
public static void main(String[] args)  
{  

    String readfile = "d:" + File.separator + "readfile.properties";  
    String writefile = "d:" + File.separator + "writefile.properties";  
    String readxmlfile = "d:" + File.separator + "readxmlfile.xml";  
    String writexmlfile = "d:" + File.separator + "writexmlfile.xml";  
    String readtxtfile = "d:" + File.separator + "readtxtfile.txt";  
    String writetxtfile = "d:" + File.separator + "writetxtfile.txt";  

    readPropertiesFile(readfile); //读取properties文件  
    writePropertiesFile(writefile); //写properties文件  
    readPropertiesFileFromXML(readxmlfile); //读取XML文件  
    writePropertiesFileToXML(writexmlfile); //写XML文件  
    readPropertiesFile(readtxtfile); //读取txt文件  
    writePropertiesFile(writetxtfile); //写txt文件  
}  

//读取资源文件,并处理中文乱码  
public static void readPropertiesFile(String filename)  
{  
    Properties properties = new Properties();  
    try  
    {  
        InputStream inputStream = new FileInputStream(filename);  
        properties.load(inputStream);  
        inputStream.close(); //关闭流  
    }  
    catch (IOException e)  
    {  
        e.printStackTrace();  
    }  
    String username = properties.getProperty("username");  
    String passsword = properties.getProperty("password");  
    String chinese = properties.getProperty("chinese");  
    try  
    {  
        chinese = new String(chinese.getBytes("ISO-8859-1"), "GBK"); // 处理中文乱码  
    }  
    catch (UnsupportedEncodingException e)  
    {  
        e.printStackTrace();  
    }  
    System.out.println(username);  
    System.out.println(passsword);  
    System.out.println(chinese);  
}  

//读取XML文件,并处理中文乱码  
public static void readPropertiesFileFromXML(String filename)  
{  
    Properties properties = new Properties();  
    try  
    {  
        InputStream inputStream = new FileInputStream(filename);  
        properties.loadFromXML(inputStream);  
        inputStream.close();  
    }  
    catch (IOException e)  
    {  
        e.printStackTrace();  
    }  
    String username = properties.getProperty("username");  
    String passsword = properties.getProperty("password");  
    String chinese = properties.getProperty("chinese"); //XML中的中文不用处理乱码，正常显示  
    System.out.println(username);  
    System.out.println(passsword);  
    System.out.println(chinese);  
}  

//写资源文件，含中文  
public static void writePropertiesFile(String filename)  
{  
    Properties properties = new Properties();  
    try  
    {  
        OutputStream outputStream = new FileOutputStream(filename);  
        properties.setProperty("username", "myname");  
        properties.setProperty("password", "mypassword");  
        properties.setProperty("chinese", "中文");  
        properties.store(outputStream, "author: shixing_11@sina.com");  
        outputStream.close();  
    }  
    catch (IOException e)  
    {  
        e.printStackTrace();  
    }  
}  

//写资源文件到XML文件，含中文    
public static void writePropertiesFileToXML(String filename)  
{  
    Properties properties = new Properties();  
    try  
    {  
        OutputStream outputStream = new FileOutputStream(filename);  
        properties.setProperty("username", "myname");  
        properties.setProperty("password", "mypassword");  
        properties.setProperty("chinese", "中文");  
        properties.storeToXML(outputStream, "author: shixing_11@sina.com");  
        outputStream.close();  
    }  
    catch (IOException e)  
    {  
        e.printStackTrace();  
    }  
}  
}